package com.webmall.servlet.user;

import com.webmall.entity.UserEntity;
import com.webmall.service.webmall_UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/adminLogin")
public class adminLogin extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //设置字符集
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

        String id = request.getParameter("id");
        String pwd = request.getParameter("pwd");

        UserEntity user = webmall_UserDao.selectById(id);
        PrintWriter out = response.getWriter();
        if (null != user){
            String true_pwd =user.getUSER_PWD();
            System.out.println(true_pwd);



            if (pwd.equals(true_pwd)){

                HttpSession session = request.getSession();
                session.setAttribute("login_user",user);//把登录的user保存进session
                session.setAttribute("isLogin","1");//把登录标志位也保存进session
                if (user.getUSER_STATUS() ==1) {
                    out.write("<script>");
                    out.write("alert('非管理员，无权限进后台管理');");
                    out.write("location.href='login.jsp';");
                    out.write("</script>");
                    //response.sendRedirect("/Webmall/index.jsp");//成功

                }else {

                    session.setAttribute("isAdmin","1");
                    response.sendRedirect("/manageHtml/admin_index.jsp");//成功
                }


            }else {

                out.write("<script>");
                out.write("alert('用户名或密码错误');");
                out.write("location.href='login.jsp';");
                out.write("</script>");
            }

        }else {
            out.write("<script>");
            out.write("alert('无该用户');");
            out.write("location.href='manageHtml/adminLogin.jsp';");
            out.write("</script>");
        }

    }





}
